<?php
/**
 * Created by PhpStorm.
 * User: THUNDEROBOT
 * Date: 2020/10/23
 * Time: 18:38
 */

namespace app\admin\controller;

use app\admin\model\Student;
use app\admin\model\Teacher;
use app\admin\validate\Teachers;
use think\captcha\Captcha;
use think\Controller;
use think\Cookie;
use think\Request;
use think\Session;

class Login extends Controller
{
    public function index(){
        return $this->fetch();
    }

    public function doLogin()
    {
        if (Request::instance()->isPost()) {
            $formData = Request::instance()->post();

            $abc=new Teachers();
            if(!$abc->check($formData)){
                $this->error($abc->getError());
            }else{
                if(preg_match("/^-?[0-9]+$/",$formData['form-username'])){

                    $usersModel = new Teacher();
                    $userData = $usersModel->checkUser($formData['form-username'],$formData['form-password']);
                    if($userData){
                        $this->success("登陆成功！",'admin/index/index');
    //                    header("location:'F:\myphp\D3\exam\application\admin\controller\index.php'");
                    }else{
                        echo "，登陆失败！";
                    }
                }else{
                    $usersModel = new Student();
                    $userData = $usersModel->checkUser($formData['form-username'],$formData['form-password']);
                    if($userData){
                        Session::set('form-username',$formData['form-username']);
                        Cookie::set('form-username',$formData['form-username']);
                        $this->success("登陆成功！",'admin/index2/index');
                        //                    header("location:'F:\myphp\D3\exam\application\admin\controller\index.php'");
                    }else{
                        echo "，登陆失败！";
                    }

                }
            }

        } else {
            $this->error("只能用Post方法");}
        }
        public function verify(){
        $config = [
            "length" =>4,
            "codeSet" => "0123456789",
            "fontSize" =>"18",
            "useNoise" => false,
            "useZh"=>true,
            "zhSet" => "1234"
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
}





}